import axios from "axios"
import { createAsyncThunk } from "@reduxjs/toolkit"
import AsyncStorage from "@react-native-async-storage/async-storage"

const backendURL = "https://backend-dot-konav2-dev.wl.r.appspot.com"

//TODO add pagination
export const getAppointment = createAsyncThunk(
  "get/Appointment",
  async ({ offset }: { offset: any }, { rejectWithValue }) => {
    try {
      const token = await AsyncStorage.getItem("token")
      const config = {
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${token}`,
        },
      }
      const response = await axios.get(
        `${backendURL}/api/calendar-list/get-appointments?offset=${offset}&limit=10&ordering=start_date`,
        config,
      )
      return response?.data
    } catch (error) {
      if (error.response && error.response.data.message) {
        console.log("error", error)
        return rejectWithValue(error.response.data.message)
      } else {
        return rejectWithValue(error.message)
      }
    }
  },
)
